#!/usr/bin/env python
#coding=utf-8
#是不是可以用这两种方式来判断是否所有的子线程都已经结果：判断isAlive或者枚举id求交集

import threading
import time

def func1(thno):
    icount=0
    while icount<10:
        icount += 1
        info='thread %d:info %d' % (thno,icount)
        print info
        time.sleep(0.5)
    info='thread %d exiting' % thno
    print info

def main():
    threads=[]
    for i in range(3):
        th=threading.Thread(target=func1,name='func1',args=(i,))
        th.start()
        #创建的线程对象及id保存到变量
        threads.append([th,th.ident])

    #线程id列表
    thidlist=[x[0].ident for x in threads]
    icount=0
    while icount<10:
        icount += 1

        #枚举alive线程id
        thidnume=[x.ident for x in threading.enumerate()]
        info='thidnume:%s' % str(thidnume)
        print info
        info='thidlist:%s' % str(thidlist)
        print info

        for thread in threads:
            thid=thread[0].ident
            info=thid,' isAlive=',thread[0].isAlive()
            print info
            if thid in thidnume:
                info='thread %d is running' % thid
                print info
            else:
                info='thread %d is stopped' % thid
                print info

        #集合操作求交集
        result=set(thidlist) & set(thidnume)
        info='set intersection result:%s length:%d' % (str(result),len(result))
        print info
        if len(result)<=0:
            break
        time.sleep(1)
    time.sleep(1)    
    print 'exiting main'

if __name__ == '__main__':
    main()
    